<template>
  <div id="project-wrap">
    <div class="add-project">
      <el-button icon="iconfont icon-jiufuqianbaoicon06" size="mini" class="exportBtn add" @click="addProject"
        >添加工程</el-button
      >
    </div>
    <div class="list-container">
      <div class="tables">
        <el-table
          :data="tableData"
          class="table"
          height="100%"
          ref="jdjgTable"
          @sort-change="sortChange"
          :header-cell-style="{
            padding: '0px',
            background: 'linear-gradient(0deg, #F4F6F9, #EAECF4, #F3F6F9)'
          }"
          :row-class-name="tabRowClassName"
          header-row-class-name="mapTable_th"
        >
          <el-table-column width="60" align="center" type="index" :index="addIndex" label="序号">
            <el-table-column width="60" align="center" type="index" label="检索"> </el-table-column>
          </el-table-column>
          <el-table-column label="工程名称" align="center" width="150">
            <el-table-column width="150" align="center" sortable="custom" prop="proName">
              <template slot="header" align="center" slot-scope="{}">
                <div class="searchBox" @click.stop>
                  <el-input v-model.trim="searchPositionName" @change="getDataChange()"></el-input>
                </div>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="工程编号" align="center" width="150">
            <el-table-column width="150" align="center" sortable="custom" prop="proNo">
              <template slot="header" align="center" slot-scope="{}">
                <div class="searchBox" @click.stop>
                  <el-input v-model.trim="searchPositionName" @change="getDataChange()"></el-input>
                </div>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="工程类型" align="center" width="150">
            <el-table-column width="150" align="center" sortable="custom" prop="proType">
              <template slot="header" align="center" slot-scope="{}">
                <div class="searchBox" @click.stop>
                  <el-input v-model.trim="searchPositionName" @change="getDataChange()"></el-input>
                </div>
              </template>
              <template slot-scope="scope">
                <div>
                  {{ scope.row.proType | proTypeFilter }}
                </div>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="建设单位" align="center" width="150">
            <el-table-column width="150" align="center" sortable="custom" prop="developmentUnit">
              <template slot="header" align="center" slot-scope="{}">
                <div class="searchBox" @click.stop>
                  <el-input v-model.trim="searchPositionName" @change="getDataChange()"></el-input>
                </div>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="施工单位" align="center" width="150">
            <el-table-column width="180" align="center" sortable="custom" prop="constructUnit">
              <template slot="header" align="center" slot-scope="{}">
                <div class="searchBox" @click.stop>
                  <el-input v-model.trim="searchPositionName" @change="getDataChange()"></el-input>
                </div>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="工程位置" align="center" width="150">
            <el-table-column align="center" prop="proAddress" width="150">
              <template slot="header" align="center" slot-scope="{}">
                <div class="searchBox" @click.stop>
                  <el-input v-model.trim="searchPositionName" @change="getDataChange()"></el-input>
                </div>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="80">
            <el-table-column align="center" width="80">
              <template slot-scope="scope">
                <div class="">
                  <el-button type="text" class="xg czBtn" size="small" @click="handleClick(scope.row)">
                    查看
                  </el-button>
                </div>
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
      </div>
      <div class="pags">
        <element-pagination @getData="getPagChange" :Total="total"> </element-pagination>
      </div>
    </div>
    <el-dialog
      :width="title === '添加工程' ? '52.5%' : '35%'"
      class="project-dialog"
      v-if="projectVisible"
      :center="true"
      :close-on-click-modal="false"
      :title="title"
      :before-close="closeDialog"
      :visible.sync="projectVisible"
    >
      <div class="dialog-wrap">
        <div class="left">
          <el-form ref="form" class="infoForm" label-width="90px">
            <el-form-item label="工程编号:" required v-if="title === '添加工程'">
              <!-- <span style="color: #999999">保存后自动生成</span> -->
              <el-input v-model.trim="projectData.proNo" placeholder="请输入工程编号" size="small "> </el-input>
            </el-form-item>
            <el-form-item label="工程编号:" v-else>
              {{ projectData.proNo }}
            </el-form-item>
            <el-form-item label="建设单位:" required v-if="title === '添加工程'">
              <el-input v-model.trim="projectData.developmentUnit" placeholder="请输入建设单位" size="small ">
              </el-input>
            </el-form-item>
            <el-form-item label="建设单位:" v-else>
              {{ projectData.developmentUnit }}
            </el-form-item>
            <el-form-item label="施工单位:" required v-if="title === '添加工程'">
              <el-input v-model.trim="projectData.constructUnit" placeholder="请输入施工单位" size="small "> </el-input>
            </el-form-item>
            <el-form-item label="施工单位:" v-else>
              {{ projectData.constructUnit }}
            </el-form-item>
            <el-form-item label="监理单位:" required v-if="title === '添加工程'">
              <el-input v-model.trim="projectData.constructControlUnit" placeholder="请输入监理单位" size="small ">
              </el-input>
            </el-form-item>
            <el-form-item label="监理单位:" v-else>
              {{ projectData.constructControlUnit }}
            </el-form-item>
            <el-form-item label="设计单位:" required v-if="title === '添加工程'">
              <el-input v-model.trim="projectData.designUnit" placeholder="请输入设计单位" size="small "> </el-input>
            </el-form-item>
            <el-form-item label="设计单位:" v-else>
              {{ projectData.designUnit }}
            </el-form-item>
            <el-form-item label="工程位置:" required v-if="title === '添加工程'">
              <el-input v-model.trim="projectData.proAddress" placeholder="请输入工程位置" size="small "> </el-input>
            </el-form-item>
            <el-form-item label="工程位置:" v-else>
              {{ projectData.proAddress }}
            </el-form-item>
          </el-form>
        </div>
        <div class="right">
          <el-form ref="form" class="infoForm" label-width="150px">
            <el-form-item label="工程名称:" required v-if="title === '添加工程'">
              <el-input v-model.trim="projectData.proName" placeholder="请输入工程名称" size="small "> </el-input>
            </el-form-item>
            <el-form-item label="工程名称:" v-else>
              {{ projectData.proName }}
            </el-form-item>
            <el-form-item label="施工许可证:" required v-if="title === '添加工程'">
              <el-input v-model.trim="projectData.buildLicense" placeholder="请输入施工许可证" size="small ">
              </el-input>
            </el-form-item>
            <el-form-item label="施工许可证:" v-else>
              {{ projectData.buildLicense }}
            </el-form-item>
            <el-form-item label="施工单位超管账号:" required v-if="title === '添加工程'">
              <div class="adminaccount">
                <el-input
                  v-model.trim="projectData.account"
                  clearable
                  placeholder="请输入施工单位超管账号"
                  size="small "
                >
                </el-input>
                <el-button type="text" style="margin-left: 10px" class="">设置密码</el-button>
              </div>
            </el-form-item>
            <el-form-item label="施工单位超管账号:" v-else>
              {{ projectData.account }}
            </el-form-item>
            <el-form-item label="勘察单位:" required v-if="title === '添加工程'">
              <el-input v-model.trim="projectData.surveyUnit" placeholder="请输入勘察单位" size="small "> </el-input>
            </el-form-item>
            <el-form-item label="勘察单位:" v-else>
              {{ projectData.surveyUnit }}
            </el-form-item>
            <el-form-item label="工程类型:" required v-if="title === '添加工程'">
              <el-select size="small" v-model.trim="projectData.proType" placeholder="请选择工程类型">
                <el-option v-for="item in proTypeOptions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="工程类型:" v-else>
              {{ projectData.proType | proTypeFilter }}
            </el-form-item>
          </el-form>
        </div>
      </div>
      <dialog-footer-button @saveName="saveName" @close="close" v-if="title === '添加工程'"> </dialog-footer-button>
    </el-dialog>
  </div>
</template>

<script>
import ElementPagination from '../../../components/ElementPagination.vue'
import dialogFooterButton from '../../../components/dialogFooterButton.vue'
export default {
  components: { ElementPagination, dialogFooterButton },
  data() {
    return {
      title: '',
      tableData: [],
      searchPositionName: '',
      projectVisible: false,
      proTypeOptions: [
        {
          label: '房屋建筑工程',
          value: '1'
        },
        {
          label: '市政公用工程',
          value: '2'
        },
        {
          label: '电力工程',
          value: '3'
        },
        {
          label: '二次装修工程',
          value: '4'
        }
      ],
      total: 0,
      size: 15,
      page: 1,
      projectData: {
        proNo: '',
        account: '',
        buildLicense: '',
        constructControlUnit: '',
        constructUnit: '',
        designUnit: '',
        developmentUnit: '',
        proAddress: '',
        proName: '',
        surveyUnit: '',
        proType: ''
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    handleClick(scope) {
      this.title = '查看工程 '
      this.projectVisible = true
      this.projectData = {
        proNo: scope.proNo,
        account: scope.account,
        buildLicense: scope.buildLicense,
        constructControlUnit: scope.constructControlUnit,
        constructUnit: scope.constructUnit,
        designUnit: scope.designUnit,
        developmentUnit: scope.developmentUnit,
        proAddress: scope.proAddress,
        proName: scope.proName,
        surveyUnit: scope.surveyUnit,
        proType: scope.proType
      }
    },
    // 添加工程
    async saveName() {
      let reg = /[\u4e00-\u9fa5]/g
      // 查看该对象中的属性是否存在空值
      let flag = await Object.values(this.projectData).some(item => {
        return !item
      })
      if (flag) {
        this.$message.error('带*号文本不能为空，请输入或者选择')
      } else {
        if (reg.test(this.projectData.account)) {
          this.$message.error('账号不能为汉字且长度不能超过15,请重新输入')
          this.projectData.account = ''
        } else if (this.projectData.account.length > 15) {
          this.$message.error('账号长度不能超过15,请重新输入')
          this.projectData.account = ''
        } else {
          this.$https({
            method: 'post',
            data: this.projectData,
            url: '/government/addProject'
          })
            .then(res => {
              if (res.code === 0) {
                this.getData()
                this.close()
                this.$message.success(res.msg)
              } else {
                this.$message.error(res.msg)
              }
            })
            .catch(err => {
              console.log(err)
            })
        }
      }
    },
    // 开启对话框
    addProject() {
      this.title = '添加工程'
      this.projectVisible = true
    },
    // 分页改变时触发事件 由子组件触发
    getPagChange(data) {
      this.page = data.currentPage
      this.size = data.pagesize
      this.getData()
    },
    getDataChange() {},
    // 关闭对话框回调
    closeDialog(done) {
      this.projectData = {
        account: '',
        buildLicense: '',
        constructControlUnit: '',
        constructUnit: '',
        designUnit: '',
        developmentUnit: '',
        proAddress: '',
        proName: '',
        proNo: '',
        surveyUnit: '',
        proType: ''
      }
      done()
    },
    // 取消按钮关闭对话框
    close() {
      this.projectData = {
        account: '',
        buildLicense: '',
        constructControlUnit: '',
        constructUnit: '',
        designUnit: '',
        developmentUnit: '',
        proAddress: '',
        proName: '',
        proNo: '',
        surveyUnit: '',
        proType: ''
      }
      this.projectVisible = false
    },
    getData() {
      this.$https({
        method: 'post',
        url: '/government/listProject',
        data: {
          page: this.page,
          size: this.size
        }
      })
        .then(res => {
          if (res.code === 0) {
            this.total = res.data.total
            this.tableData = res.data.records
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    sortChange() {},
    addIndex(index) {
      return index + 1 + (this.page - 1) * this.size
    },
    tabRowClassName({ row, rowIndex }) {
      let index = rowIndex + 1
      if (index % 2 == 0) {
        return 'warning-row'
      }
    }
  },
  filters: {
    proTypeFilter(val) {
      switch (val) {
        case '1':
          return '房屋建筑工程'
        case '2':
          return '市政公用工程'
        case '3':
          return '电力工程'
        case '4':
          return '二次装修工程'
        default:
          break
      }
    }
  }
}
</script>
<style lang="less" scoped>
#project-wrap {
  height: 100%;
  .add-project {
    display: flex;
    justify-content: flex-end;
    padding: 16px;
  }
  .xz {
    width: 60px;
  }
  .add {
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    width: 114px;
  }
  .list-container {
    height: calc(100% - 104px);
    padding: 0px 16px 40px 16px;
    .tables {
      height: calc(100% - 62px);
    }
  }
  .project-dialog {
    .dialog-wrap {
      display: flex;
      .left {
        padding-right: 50px;
      }
      .adminaccount {
        display: flex;
      }
    }
  }
}
</style>
<style lang="less">
#project-wrap {
  .project-dialog {
    .el-form-item {
      margin-bottom: 15px;
    }
    .el-form-item__label {
      color: #333;
    }
    .el-input__inner {
      width: 280px;
    }
    .select {
      .el-input__inner {
        width: 150px;
      }
    }
  }
}
</style>
